﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Data;

namespace NetChatInterface
{
    class Text
    {


        //为服务器
        private Socket s;   //定义的socket对象
        private Thread th;  //客户端连接服务器的线程
        public Socket CSocket;    //单个客户端连接服务器的socket
        public NetworkStream ns;//网络流
        public StreamReader sr; //读入流
        public StreamWriter sw; //写入流
        //private delegate void SetTextCallback(RichTextBox temp);

        //为客户端
        public Socket s1;
        public NetworkStream ns1;
        public StreamReader sr1;
        public StreamWriter sw1;

        RichTextBox input;
        RichTextBox output;

        //
        System.Media.SoundPlayer music;
        //sessionform
        //  public SessionForm sessionform=new SessionForm(IntPtr.Zero,String.Empty);
        //对方的ip信息
        // MainForm.IpHandle iphandle;

        //public Text()
        //{

        //}

        public Text(RichTextBox first, RichTextBox second)
        {
            input = first;
            output = second;
        }

        public void communication_1()
        {
            communication(input, output);
        }
        public void communication(RichTextBox input, RichTextBox output)
        {
            while (true)
            {
                try
                {
                    CSocket = s.Accept();
                    if (CSocket.Connected)   //
                    {
                        ns = new NetworkStream(CSocket);
                        sr = new StreamReader(ns);
                        sw = new StreamWriter(ns);
                        //test();
                        send(output);
                        sw.WriteLine(input.Text);

                        sw.Flush();
                    }
                    else
                    {

                        MessageBox.Show("连接失败！");
                    }

                }
                catch (SocketException s)
                {
                    MessageBox.Show(s.Message);
                }
                catch (IOException io)
                {
                    MessageBox.Show(io.Message);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("其他异常" + ex.Message);
                }
            }

        }

        public void send(RichTextBox output)
        {
            // lbInfo.Items.Add(sr.ReadLine()+"\n");
            output.AppendText(sr.ReadLine() + "\n");
            String select = comboBox1.Text;
            switch (select)
            {
                case "sound1":
                    music = new System.Media.SoundPlayer(@"..\\..\\Resources\\START.wav");
                    music.Play();
                    break;
                case "sound2":
                    music = new System.Media.SoundPlayer(@"..\\..\\Resources\\THUD.wav");
                    music.Play();
                    break;
                case "sound3":
                    music = new System.Media.SoundPlayer(@"..\\..\\Resources\\TRASH.wav");
                    music.Play();
                    break;
                case "sound4":
                    music = new System.Media.SoundPlayer(@"..\\..\\Resources\\TR917.wav");
                    music.Play();
                    break;
                case "sound5":
                    music = new System.Media.SoundPlayer(@"..\\..\\Resources\\THUD5.wav");
                    music.Play();
                    break;
                case "sound6":
                    music = new System.Media.SoundPlayer(@"..\\..\\Resources\\ZR883.wav");
                    music.Play();
                    break;
                case "sound7":
                    music = new System.Media.SoundPlayer(@"..\\..\\Resources\\GONG3.wav");
                    music.Play();
                    break;
                default:
                    break;
            }
           
        }

        //public void test()
        //{
        //    SetTextCallback stcb=new SetTextCallback(send);
        // //   Invoke(stcb);
        //}

        //public Test()
        //{
        //    InitializeComponent();
        //}

        public void connect_server()
        {
            // button1.Enabled=false;
            s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            String hostname;
            hostname = Dns.GetHostName();
            IPAddress ServerIp = Dns.Resolve(hostname).AddressList[0];

            //  IPAddress ServerIp=IPAddress.Parse("222.178.10.246");
            IPEndPoint server = new IPEndPoint(ServerIp, 15);
            s.Bind(server);
            s.Listen(10);
            try
            {

                th = new Thread(new ThreadStart(communication_1));
                th.Start();
                //  label1.Text="服务器连接成功";
            }
            catch (Exception ex)
            {
                MessageBox.Show("message error" + ex.Message);
            }
        }

        //private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        //{
        //  //  s.Shutdown(SocketShutdown.Both);
        //  //  s.Close();
        //  //  s1.Shutdown(SocketShutdown.Both);
        //   // s1.Close();
        //   // this.Close();
        //}

        public void send_msg(String ip, RichTextBox input, RichTextBox output)
        {
            s1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //////////////////////////////////////////////////
            String hostname;
            hostname = Dns.GetHostName();
            IPAddress host_Ip = Dns.Resolve(hostname).AddressList[0];
            //////////////////////////////////////////////////
            IPAddress serverIp = IPAddress.Parse(ip);
            try
            {
                s1.Connect(serverIp, 13);   //使用其他端口

            }
            catch (Exception ex)
            {
                MessageBox.Show("message error:" + ex.Message);
            }
            try
            {
                ns1 = new NetworkStream(s1);
                sr1 = new StreamReader(ns1);
                sw1 = new StreamWriter(ns1);
                sw1.WriteLine(host_Ip + " <" + DateTime.Now.ToString() + ">: " + input.Text);
                sw1.Flush();
                //  lbInfo.Items.Add("宝宝："+richTextBox1.Text);
                output.AppendText(host_Ip + " <" + DateTime.Now.ToString() + ">: " + input.Text);
                input.Text = "";

                //  lbInfo.Items.Add(sr1.ReadLine());
                output.AppendText(sr1.ReadLine() + "\n");
            }
            catch (Exception ei)
            {
                MessageBox.Show("message error" + ei.Message);
            }
        }

    }
}
